﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_application : System.Web.UI.Page
{
    string opcion;
    DataSet permisos;
    Table tablaOpp;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
        //Si es la primera vez que entra


        if (!Page.IsPostBack)
        {

            try { opcion = Request.QueryString["Id"].ToString(); }
            catch (Exception oex) { opcion = null; }

            if (opcion == null) { Response.Redirect("Applications.aspx"); }
            // if (opcion == null) { opcion = "1"; }

            if (Session["permisos"] != null)
            {
                crearPagPermisos(opcion);
            }

        }
        else
        {
            opcion = Request.QueryString["Id"].ToString();
            //if (opcion != null)
                //crearPagPermisos(opcion);
        }
    }

    protected void Page_Init(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            opcion = Request.QueryString["Id"].ToString();
            if (opcion != null)
                crearPagPermisos(opcion);
        }
    }

    private void pintarOpciones()
    {
        /*com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        int o = int.Parse(opcion);
       // string pagop = "";
        string pagop = service.getAppOptions(o);
        
       // string pagop = "";
        DataSet pagopp = Utility.getPermitOption(pagop);

        Table table = new Table();
        table.Controls.Clear();
        table.CellPadding = 0;
        table.CellSpacing = 0;
        table.Width=250;
        TableHeaderRow thr = new TableHeaderRow();
        TableHeaderCell thc1 = new TableHeaderCell();
        thc1.Text = "Option";
        thc1.Width = 180;
        thc1.BackColor = System.Drawing.Color.FromName("#9AC9F8");
        TableHeaderCell thc2 = new TableHeaderCell();
        thc2.Text = "Edit";
        thc2.Width = 70;
        thc2.BackColor = System.Drawing.Color.FromName("#9AC9F8");
        thr.Cells.Add(thc1);
        thr.Cells.Add(thc2);
       // table.Rows.Add(thr);



        for (int i = 0; i <= (pagopp.Tables[0].Rows.Count - 1); i++)
        {
            TableRow tr = new TableRow();
            
                if (i % 2 == 0){
                    tr.BackColor = System.Drawing.Color.FromName("#CBE2FE");
                }
                int id = int.Parse(pagopp.Tables["tablaOptions"].Rows[i]["id_option"].ToString());
                TableCell tc1 = new TableCell();
                Label lb = new Label();
                lb.ID = "tb_" + pagopp.Tables["tablaOptions"].Rows[i]["id_option"].ToString();
                lb.CssClass = "letraAzul14";
                lb.Text = pagopp.Tables["tablaOptions"].Rows[i]["name_option"].ToString();
                tc1.Controls.Add(lb);
                tc1.Width = 180;
                TableCell tc2 = new TableCell();

                tc2.Text = "<a href= \"editoption.aspx?id=" + pagopp.Tables["tablaOptions"].Rows[i]["id_option"].ToString() + "&parent=" + opcion +"\">Edit</a>";
                tc2.Width = 70;
                thc2.HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc1);
                tr.Cells.Add(tc2);
                string nombre = pagopp.Tables["tablaOptions"].Rows[i]["parent_name"].ToString();
                tb_role.Text = nombre; 
                long per = long.Parse(pagopp.Tables["tablaOptions"].Rows[i]["permit"].ToString());
                if (per != 1) {
                    table.Rows.Add(tr);
                }

        }


        //Panel1.Controls.Add(table); */
    }

    private void deleteOptionDin(int id)
    {
        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        service.deleteOption(id.ToString(), opcion);
        Response.Redirect("aplication.aspx");
        
    }

    private string crearxml()
    {
        string result = "";
        opcion = Request.QueryString["Id"].ToString();
        /*result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><opcionid>" + opcion + "</opcionid><app>1</app>";
        result += "<OptionsAccess>";
        foreach (ListItem item in ChkListAccess.Items)
        {
            if (item.Selected)
            {
                result += "<option>" + item.Value.ToString() + " </option>";
            }
        }
        result += "</OptionsAccess>";

        result += "</data>";*/

        if (tb_role.Text != "")
        {
            result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><opcionid>" + opcion + "</opcionid><app>1</app>";
            result += "<opcionname>" + tb_role.Text + "</opcionname>";
            result += "</data>";
        }
        return result;
    }

    private string crearxml2()
    {
        string result = "";
        if (Session["SID_page"] != null && Session["SID_option"] != null)
        {
            string idpage = (string)Session["SID_page"];
            string idoption = (string)Session["SID_option"];
            result += "<?xml version=\"1.0\" encoding=\"utf-8\"?><data><opcionid>" + idoption + "</opcionid><parent>" + idpage + "</parent>";
            result += "<opcionname>" + tb_role2.Text + "</opcionname>";
            if (chk_movCat.Checked == true)
                result += "<opcsection>" + rb_move_cat.SelectedValue + "</opcsection>";
            else
                result += "<opcsection>0</opcsection>";
            result += "</data>";
        }
        return result;
    }

    protected void lnk_save_Click(object sender, EventArgs e)
    {
        string sxml = crearxml();

        com.bpophotos.www.Service service = new com.bpophotos.www.Service();
        int x= service.updateDataPage(sxml);
        service.Dispose();
        Response.Redirect("Applications.aspx");
    }
    protected void lnk_delete_Click(object sender, EventArgs e)
    {
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null)
            Response.Redirect("deleteOption.aspx?id=" + opcion);
    }
    protected void lnk_addoption_Click(object sender, EventArgs e)
    {
        Label1.Visible = false;

        opcion = Request.QueryString["Id"].ToString();
        if (!tb_application.Text.Equals("") && !opcion.Equals(""))
        {

            string sxml = "";
            sxml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?> <opcion><name>";
            sxml += tb_application.Text + "</name>";
            sxml += "<parent>" + opcion + "</parent>";
            string aux = rb_cat.SelectedItem.ToString();
            if (rb_cat.SelectedIndex > -1)
                sxml += "<section>" + rb_cat.SelectedValue + "</section>";
            sxml += "<app>1</app>";
            sxml += "</opcion>";

            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            int res = service.registraOption(sxml);

            opcion = Request.QueryString["Id"].ToString();
            if (opcion != null)
                crearPagPermisos(opcion);
            Accordion1.SelectedIndex = 0;
        }
        else
        {
            Label1.Visible = true;
            bthide_ModalPopupExtender.Show();
        }
        
    }

    private void crearPagPermisos(string pageID)
    {
        try
        {
            string rxml = null;
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            rxml = service.getAppPages(1);
            if (!rxml.Equals("") || rxml != null)
            {
                DataSet dsclient = Utility.getPermitPage(rxml);
                if (dsclient != null)
                {
                    for (int i = 0; i < dsclient.Tables[0].Rows.Count; i++)
                    {
                        if (dsclient.Tables[0].Rows[i]["id_page"].ToString() == pageID)
                        {
                            if (Panel1.Controls.Contains(tablaOpp))
                                Panel1.Controls.Remove(tablaOpp);
                            tablaOpp = new Table();
                            //tablaOpp.Width = 200;
                            tablaOpp.CellPadding = 5;
                            tablaOpp.HorizontalAlign = HorizontalAlign.Left;
                            tablaOpp.Font.Size = 9;
                            tablaOpp.Font.Name = "Arial";
                            TableRow row1 = new TableRow();
                            row1.Cells.Add(rowPagina(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["image"].ToString()));
                            tablaOpp.Controls.Add(row1);
                            tablaOpp.HorizontalAlign = HorizontalAlign.Center;
                            //TableRow rows = new TableRow();

                            Panel1.Controls.Add(tablaOpp);
                            newSection(dsclient.Tables[0].Rows[i]["id_page"].ToString());
                            newPage(dsclient.Tables[0].Rows[i]["id_page"].ToString(), dsclient.Tables[0].Rows[i]["name_page"].ToString(), dsclient.Tables[0].Rows[i]["permit"].ToString(), dsclient, dsclient.Tables[0].Rows[i]["id_option"].ToString());

                            /*TableRow rows2 = new TableRow();
                            TableCell cell = new TableCell();
                            cell.Controls.Add(lnk_save);
                            cell.Controls.Add(lnk_cancel);
                            rows2.Cells.Add(cell);
                            rows2.Attributes.Add("Style", "text-align:center");
                            tablaOpp.Controls.Add(rows2);*/
                            tablaOpp.ID = "tabla" + dsclient.Tables[0].Rows[i]["id_page"].ToString();
                            tablaOpp.BorderColor = System.Drawing.Color.Black;
                            tablaOpp.BorderStyle = BorderStyle.Ridge;
                            tablaOpp.Attributes.Add("Style", "box-shadow: 0 0 20px 3px #000000;");
                            break;
                        }
                    }
                    //Panel1.BorderColor = System.Drawing.Color.Black;
                    //Panel1.BorderStyle = BorderStyle.Ridge;

                    fillCategory();
                }
            }
        }
        catch (Exception ex)
        {
            Utility.writeError("Error al obtener datos paginas WEBSERVICE application.aspx " + ex.Message + " " + ex.StackTrace);
        }
    }

    private TableCell rowPagina(string idPage, string namePage, string imaPage)
    {
        TableCell result = new TableCell();

        Table table = new Table();
        TableRow row1 = new TableRow();
        TableCell cell1 = new TableCell();
        cell1.Width = 60;
        cell1.Height = 60;
        cell1.HorizontalAlign = HorizontalAlign.Left;
        ImageButton imageButton = new ImageButton();
        imageButton.ID = "btn_" + idPage;
        imageButton.BorderWidth = 0;

        string auxOver = imaPage.Replace(".", "2.");
        imageButton.Attributes.Add("onmouseout", "this.src='../img/ImaPages/" + imaPage + "'");
        imageButton.Attributes.Add("onmouseover", "this.src='../img/ImaPages/" + auxOver + "'");

        if (!imaPage.Equals(""))
        {
            imageButton.ImageUrl = "../img/ImaPages/" + imaPage;
        }
        cell1.Controls.Add(imageButton);
        row1.Cells.Add(cell1);
        table.Controls.Add(row1);

        TableCell cell2 = new TableCell();
        cell2.Width = 290;
        cell2.Height = 20;
        cell2.HorizontalAlign = HorizontalAlign.Left;
        if (tb_role.Text == "")
            tb_role.Text = namePage;
        tb_role.Font.Bold = true;
        tb_role.Font.Size = 14;
        tb_role.Font.Name = "Arial";
        tb_role.BorderStyle = BorderStyle.None;
        cell2.Controls.Add(tb_role);
        row1.Cells.Add(cell2);

        result.Controls.Add(table);
        return result;
    }

    public void newPage(string id_parent, string name_page, string per_page, DataSet oppPage, string id_opc)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        /* Devuelve una tabla para cada pagina
        TableCell result = new TableCell();
        Table tabla = new Table();*/

        // Permisos de la pagina
        TableRow rowPer = new TableRow();

        TableCell cellblank = new TableCell();
        cellblank.Width = 180;
        //cellblank.BorderStyle = BorderStyle.Groove;
        //cellblank.BorderWidth = 2;
        Label lnamepage = new Label();
        lnamepage.Text = name_page;
        cellblank.Controls.Add(lnamepage);
        cellblank.HorizontalAlign = HorizontalAlign.Left;
        rowPer.Cells.Add(cellblank);

        TableCell cellPer = new TableCell();
        cellPer.HorizontalAlign = HorizontalAlign.Left;
        //cellPer.Text = "<a href= \"editoption.aspx?id=" + id_opc + "&parent=" + opcion + "\">Edit</a>";
        LinkButton lnkBt = new LinkButton();
        lnkBt.ID = id_parent + "_" + id_opc + "_" + name_page;
        lnkBt.Text = "Edit";
        lnkBt.Click += new EventHandler(lnkBt_Click);
        cellPer.Controls.Add(lnkBt);
        rowPer.Cells.Add(cellPer);

        Table tableG = (Table)Accordion1.FindControl("TableSecGeneral");
        if (tableG != null)
            tableG.Controls.Add(rowPer);
        int contOpp = 0;
        TableRow rowOpp = null;
        // Opciones de la pagina
        for (int i = 0; i < oppPage.Tables[1].Rows.Count; i++)
        {
            if (oppPage.Tables[1].Rows[i]["id_page"].ToString() == id_parent)
            {
                if (section.Tables[0].Rows.Count > 1)
                {
                    contOpp++;
                    if ((contOpp % 2) != 0)
                        rowOpp = new TableRow();
                }
                else { rowOpp = new TableRow(); }

                // Nueva celda nombre opcion
                TableCell cellOpp = new TableCell();
                cellOpp.Width = 180;
                cellOpp.HorizontalAlign = HorizontalAlign.Left;
                Label lOpp = new Label();
                lOpp.Width = 180;
                lOpp.Text = oppPage.Tables[1].Rows[i]["name_option"].ToString();
                lOpp.Font.Bold = true;
                lOpp.Font.Size = 9;
                lOpp.Font.Name = "Arial";
                //lOpp.ForeColor = System.Drawing.Color.FromName("#525663");
                cellOpp.Controls.Add(lOpp);
                rowOpp.Cells.Add(cellOpp);

                // Nueva celda opcion
                TableCell cellOpp2 = new TableCell();
                cellOpp2.HorizontalAlign = HorizontalAlign.Left;
                //cellOpp2.Text = "<a href= \"editoption.aspx?id=" + oppPage.Tables[1].Rows[i]["id_option"].ToString() + "&parent=" + opcion + "\">Edit</a>";
                LinkButton lnkButton = new LinkButton();
                lnkButton.ID = id_parent + "_" + oppPage.Tables[1].Rows[i]["id_option"].ToString() + "_" + oppPage.Tables[1].Rows[i]["name_option"].ToString();
                lnkButton.Text = "Edit";
                lnkButton.Click += new EventHandler(lnkButton_Click);
                cellOpp2.Controls.Add(lnkButton);
                rowOpp.Cells.Add(cellOpp2);

                Table tableG2 = null;
                if (oppPage.Tables[1].Rows[i]["name_section"].ToString().Equals("General"))
                {
                    tableG2 = (Table)Accordion1.FindControl("TableSecGeneral");
                }
                else
                {
                    tableG2 = (Table)Accordion1.FindControl("TableSec" + oppPage.Tables[1].Rows[i]["id_section"].ToString().Trim());
                }
                if (tableG2 != null)
                {
                    tableG2.Controls.Add(rowOpp);
                    if (i < (oppPage.Tables[1].Rows.Count - 1))
                        if (int.Parse(oppPage.Tables[1].Rows[i]["id_section"].ToString()) != int.Parse(oppPage.Tables[1].Rows[i + 1]["id_section"].ToString()))
                        {
                            rowOpp = new TableRow();
                            contOpp = 0;
                        }
                    if (int.Parse(oppPage.Tables[1].Rows[i]["orden"].ToString()) == 0)
                    {
                        contOpp = 0;
                    }
                }
            }
        }
        //return result;
    }

    void lnkBt_Click(object sender, EventArgs e)
    {
        LinkButton lnk_BTP = (LinkButton)sender;
        string ID = lnk_BTP.ID;
        string[] pID = ID.Split('_');
        if (pID.Length == 3)
        {
            tb_role2.Text = pID[2];
            Session["SID_page"] = pID[0];
            Session["SID_option"] = pID[1];
            chk_movCat.Checked = false;
            lb_move_cat.Visible = false;
            rb_move_cat.Visible = false;
            Label2.Visible = false;
            bt_hide2_ModalPopupExtender1.Show();
        }
    }

    void lnkButton_Click(object sender, EventArgs e)
    {
        LinkButton lnk_BTO = (LinkButton)sender;
        string ID = lnk_BTO.ID;
        string[] oID = ID.Split('_');
        if (oID.Length == 3)
        {
            tb_role2.Text = oID[2];
            Session["SID_page"] = oID[0];
            Session["SID_option"] = oID[1];
            chk_movCat.Checked = false;
            lb_move_cat.Visible = false;
            rb_move_cat.Visible = false;
            Label2.Visible = false;
            bt_hide2_ModalPopupExtender1.Show();
        }
    }

    public void newSection(string id_parent)
    {
        DataSet section = null;
        section = DB.getSectionPage(id_parent);
        //***************************** Section *************************************************
        if (true)
        {
            Table tableP = new Table();
            // Tamaño y alineacion de la tabla
            if (section.Tables[0].Rows.Count > 1)
                tableP.Width = 550;
            tableP.CellSpacing = 0;
            tableP.HorizontalAlign = HorizontalAlign.Left;
            // Nombre y estilo de la tabla
            tableP.BorderStyle = BorderStyle.Groove;
            tableP.BorderWidth = 2;
            tableP.Font.Bold = true;

            TableRow rowP = new TableRow();
            TableCell cellP = new TableCell();
            Accordion1.Panes.Clear();
            cellP.Controls.Add(Accordion1);
            rowP.Cells.Add(cellP);
            tableP.Controls.Add(rowP);

            TableRow rowP2 = new TableRow();
            TableCell cellP2 = new TableCell();
            cellP2.Controls.Add(tableP);
            rowP2.Cells.Add(cellP2);
            tablaOpp.Controls.Add(rowP2);

            if (section != null)
            {
                for (int j = 0; j < section.Tables[0].Rows.Count; j++)
                {
                    Table tableS = new Table();

                    if (section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                    {
                        tableS.ID = "TableSecGeneral";
                    }
                    else
                    {
                        tableS.ID = "TableSec" + section.Tables[0].Rows[j]["section_id"].ToString().Trim();
                    }
                    tableS.CellSpacing = 1;
                    tableS.HorizontalAlign = HorizontalAlign.Left;
                    /*tableS.BorderStyle = BorderStyle.Solid;
                    tableS.BorderColor = System.Drawing.Color.Red;
                    tableS.BorderWidth = 2;*/
                    if (section.Tables[0].Rows.Count > 1)
                        tableS.Width = 550;
                    else
                        tableS.Width = 400;
                    Label lsection = new Label();
                    lsection.ID = "ls" + section.Tables[0].Rows[j]["section_id"].ToString();
                    lsection.Text = section.Tables[0].Rows[j]["section_name"].ToString();
                    lsection.Font.Size = 10;
                    TableCell c1 = new TableCell();
                    c1.HorizontalAlign = HorizontalAlign.Left;
                    c1.Controls.Add(lsection);
                    TableRow r1 = new TableRow();
                    r1.Cells.Add(c1);
                    if (!section.Tables[0].Rows[j]["section_name"].ToString().Trim().Equals("General"))
                        tableS.Controls.Add(r1);

                    AjaxControlToolkit.AccordionPane ap = new AjaxControlToolkit.AccordionPane();
                    ap.ID = "AP" + section.Tables[0].Rows[j]["section_id"].ToString();
                    ap.Controls[0].Controls.Add(lsection);
                    ap.Controls[1].Controls.Add(tableS);
                    Accordion1.Panes.Add(ap);
                }
            }
            Accordion1.RequireOpenedPane = true;
            Accordion1.SelectedIndex = 0;
        }
        else { section = null; }
    }
    protected void lnk_add_Click(object sender, EventArgs e)
    {
        tb_application.Text = string.Empty;
        Label1.Visible = false;
        bthide_ModalPopupExtender.Show();
    }

    public void fillCategory()
    {
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null)
        {
            DataSet categoria = DB.getCatPage(int.Parse(opcion));
            for (int i = 0; i < categoria.Tables[0].Rows.Count; i++)
            {
                if (rb_cat.Items.IndexOf(rb_cat.Items.FindByValue(categoria.Tables[0].Rows[i]["id"].ToString())) == -1)
                {
                    ListItem item = new ListItem();
                    item.Text = categoria.Tables[0].Rows[i]["name"].ToString();
                    item.Value = categoria.Tables[0].Rows[i]["id"].ToString();
                    rb_cat.Items.Add(item);
                }
            }
            if (rb_cat.Items.Count >= 0)
            {
                rb_cat.Items[0].Selected = true;
            }
            //bthide_ModalPopupExtender.Show();
        }
    }
    protected void lnk_delete_option_Click(object sender, EventArgs e)
    {
        string idOPP = (string)Session["SID_option"];
        string idPag = (string)Session["SID_page"];
        Response.Redirect("deleteOptionO.aspx?id=" + idOPP + "&parent=" + idPag);
        Session["SID_page"] = null;
        Session["SID_option"] = null;
    }
    protected void lnk_save_option_Click(object sender, EventArgs e)
    {
        opcion = Request.QueryString["Id"];
        Label2.Visible = false;
        if (tb_role2.Text != "" && opcion != null)
        {
            string sxml = crearxml2();
            com.bpophotos.www.Service service = new com.bpophotos.www.Service();
            int resp = service.updateDataOption(sxml);
            Session["SID_page"] = null;
            Session["SID_option"] = null;
            crearPagPermisos(opcion);
        }
        else
        {
            Label2.Visible = true;
            bt_hide2_ModalPopupExtender1.Show();
        }
    }
    protected void chk_movCat_CheckedChanged(object sender, EventArgs e)
    {
        if (chk_movCat.Checked == true)
        {
            lb_move_cat.Visible = true;
            rb_move_cat.Visible = true;
            fillMoveCat();
            bt_hide2_ModalPopupExtender1.Show();
        }
        else
        {
            lb_move_cat.Visible = false;
            rb_move_cat.Visible = false;
            rb_move_cat.Items.Clear();
            bt_hide2_ModalPopupExtender1.Show();
        }
    }

    public void fillMoveCat()
    {
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null)
        {
            DataSet categoria = DB.getCatPage(int.Parse(opcion));
            for (int i = 0; i < categoria.Tables[0].Rows.Count; i++)
            {
                if (rb_move_cat.Items.IndexOf(rb_move_cat.Items.FindByValue(categoria.Tables[0].Rows[i]["id"].ToString())) == -1)
                {
                    ListItem item = new ListItem();
                    item.Text = categoria.Tables[0].Rows[i]["name"].ToString();
                    item.Value = categoria.Tables[0].Rows[i]["id"].ToString();
                    rb_move_cat.Items.Add(item);
                }
            }
            if (rb_move_cat.Items.Count >= 0)
            {
                rb_move_cat.Items[0].Selected = true;
            }
        }
    }

    public void fillEditCat()
    {
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null)
        {
            DataSet categoria = DB.getCatPage(int.Parse(opcion));
            for (int i = 0; i < categoria.Tables[0].Rows.Count; i++)
            {
                if (rb_editsection.Items.IndexOf(rb_editsection.Items.FindByValue(categoria.Tables[0].Rows[i]["id"].ToString())) == -1)
                {
                    ListItem item = new ListItem();
                    item.Text = categoria.Tables[0].Rows[i]["name"].ToString();
                    item.Value = categoria.Tables[0].Rows[i]["id"].ToString();
                    rb_editsection.Items.Add(item);
                }
            }
            if (rb_editsection.Items.Count >= 0)
            {
                rb_editsection.Items[0].Selected = true;
                tb_editsection.Text = rb_editsection.SelectedItem.ToString();
            }
        }
    }

    protected void lnk_edit_section_Click(object sender, EventArgs e)
    {
        Label3.Visible = false;
        rb_editsection.Items.Clear();
        chk_editsection.Checked = false;
        lb_editsection.Visible = false;
        rb_editsection.Visible = false;
        tb_editsection.Text = string.Empty;
        LnK_AddSection.Visible = true;
        LnK_CancelSection.Visible = true;
        Lnk_SaveSection.Visible = false;
        Lnk_DeleteSection.Visible = false;
        bt_hide3_Modalpopupextender.Show();
    }

    protected void chk_editsection_CheckedChanged(object sender, EventArgs e)
    {
        if (chk_editsection.Checked == true)
        {
            lb_editsection.Visible = true;
            rb_editsection.Visible = true;
            Lnk_DeleteSection.Visible = true;
            Lnk_SaveSection.Visible = true;
            LnK_AddSection.Visible = false;
            LnK_CancelSection.Visible = false;
            Label3.Visible = false;
            fillEditCat();
            bt_hide3_Modalpopupextender.Show();
        }
        else
        {
            lb_editsection.Visible = false;
            rb_editsection.Visible = false;
            Lnk_DeleteSection.Visible = false;
            Lnk_SaveSection.Visible = false;
            LnK_AddSection.Visible = true;
            LnK_CancelSection.Visible = true;
            rb_editsection.Items.Clear();
            tb_editsection.Text = string.Empty;
            bt_hide3_Modalpopupextender.Show();
        }
    }
    protected void LnK_AddSection_Click(object sender, EventArgs e)
    {
        Label3.Visible = false;
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null && tb_editsection.Text != "")
        {
            int resp = DB.InsertSection(int.Parse(opcion), tb_editsection.Text);
            if (resp == 1)
                crearPagPermisos(opcion);
        }
        else
        {
            Label3.Visible = true;
            bt_hide3_Modalpopupextender.Show();
        }
        tb_editsection.Text = string.Empty;

    }
    protected void Lnk_SaveSection_Click(object sender, EventArgs e)
    {
        Label3.Visible = false;
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null && tb_editsection.Text != "")
        {
            if (rb_editsection.Items != null)
            {
                int section = int.Parse(rb_editsection.SelectedValue.ToString());
                int resp = DB.UpdateSection(section, tb_editsection.Text);
                if (resp == 1)
                    crearPagPermisos(opcion);
            }
        }
        else
        {
            Label3.Visible = true;
            bt_hide3_Modalpopupextender.Show();
        }
    }
    protected void Lnk_DeleteSection_Click(object sender, EventArgs e)
    {
        opcion = Request.QueryString["Id"].ToString();
        if (opcion != null && rb_editsection.Items != null)
        {
            if (rb_editsection.Items[0].Selected != rb_editsection.Items[rb_editsection.SelectedIndex].Selected)
            {
                int rep = DB.deleteSection(int.Parse(opcion), int.Parse(rb_editsection.SelectedValue.ToString()), int.Parse(rb_editsection.Items[0].Value));
                if (rep == 1)
                    crearPagPermisos(opcion);
            }
        }
        
    }
    protected void rb_editsection_SelectedIndexChanged(object sender, EventArgs e)
    {
        tb_editsection.Text = rb_editsection.SelectedItem.ToString();
        bt_hide3_Modalpopupextender.Show();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Applications.aspx");
    }
}